/*
 * @(#)MockClassLoader.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.macro.method;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import com.nhncorp.lucy.common.util.ResourceLoader;

/**
 * @author Web Platform Development Team
 */
public class MockClassLoader extends ClassLoader {

	private URL jarUrl;
	List<URL> resources  = new ArrayList<URL>();
	int flag = 0;

	@Override
	protected MockEnumeration findResources(String name) throws IOException {
		ResourceLoader resourceLoader = new ResourceLoader();
		jarUrl = resourceLoader.getResource("test.jar");
		Enumeration<JarEntry> jarEntries = new JarFile(jarUrl.getFile()).entries();
		while (jarEntries.hasMoreElements()) {
			JarEntry jarEntry = jarEntries.nextElement();
			if (jarEntry.getName().endsWith(".tmm")) {
				resources.add(new URL("jar:file:" + jarUrl.getFile() + "!/" + jarEntry.getName()));
			}
		}
		return new MockEnumeration();
	}

	class MockEnumeration implements Enumeration<URL> {
		public boolean hasMoreElements() {
			if (resources.size() <= flag) {
				return false;
			}
			if (resources.get(flag) == null) {
				return false;
			}
			return true;
		}

		public URL nextElement() {
			URL jarEntryUrl = resources.get(flag);
			flag++;
			return jarEntryUrl;
		}
	}
}
